package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: SetmealServiceImpl
 * Package: com.sky.service.impl
 * Description:套餐的实现类
 *
 * @Author: Zhang Peiyuan
 * @Create: 2024-12-19-14:43
 * @Version: v1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Resource
    private DishMapper dishMapper;
    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        // 在基础的setmeal表中添加套餐的信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        // 在setmeal_dish表中添加套餐和菜品的关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            //将获得的菜品插入到setmeal_dish表中
        if(setmealDishes != null && !setmealDishes.isEmpty()) {
            for(SetmealDish setmealDishe: setmealDishes){
                // 设置套餐的id
                setmealDishe.setSetmealId(setmeal.getId());
            }
            // 批量插入
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult querySetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 由于要求返回的结果中有分类名, 故涉及多表查询, 使用VO返回
        Page<SetmealVO> res = setmealMapper.selectPage(setmealPageQueryDTO);
        return new PageResult(res.getTotal(), res.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 处理非法情况
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE.equals(setmeal.getStatus())){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        ids.forEach(setmealId -> {
            //删除套餐表中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });
    }

    @Override
    public SetmealVO querySetmeal(Long id) {
        // 查询涉及到两个表  setmeal表和setmeal_dish表
        // 返回类型为VO, VO的数据来源于两个表, 可以拼接而来
        // 查询setmeal表
        Setmeal setmeal = setmealMapper.getById(id);

        // 查询setmeal_dish表
        List<SetmealDish> setmealDishes
                = setmealDishMapper.selectBySetmealId(id);
        // 拼接
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        // 修改涉及到两个表
        // 1. setmeal  2.setmeal_dish
        // 进行第一张表的修改
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        // 对第二张表进行修改
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 将所有菜品的setmealId设置为当前setmeal的id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        // 将对应的菜品都删除
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        // 重新加入新的菜品
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal build = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        // 关闭时没有任何要求
        if(status.equals(StatusConstant.DISABLE)){
            setmealMapper.update(build);
        }else{
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && !dishList.isEmpty()){
                for (Dish dish : dishList) {
                    if(dish.getStatus().equals(StatusConstant.DISABLE )) {
                        //有菜品关闭售,不能上架
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
            setmealMapper.update(build);
        }


    }

    /**
     * 根据分类id查询套餐列表
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> querySetmealList(Long categoryId) {
        return setmealMapper.selectListByCategoryId(categoryId);
    }

    /**
     * 查询当前套餐对应的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> querySetmealOfDish(Long id) {
        List<DishItemVO> res = setmealDishMapper.selectSetmealOfDish(id);
        return res;
    }


}
